#include "linklist.h"

SingleLinkList::SingleLinkList()
{
    list= new SingleLinkListNode;
    tail= new SingleLinkListNode;
    tail_head = new SingleLinkListNode;
//    tail_head = tail;
}

void SingleLinkList::headInsert(LinkListType data)
{
    SingleLinkListNode *node = new SingleLinkListNode;
    node->data = data;
    node->next = list->next;
    list->next = node;
}

void SingleLinkList::tailInsert(LinkListType data)
{
    SingleLinkListNode *node = new SingleLinkListNode;
    node->data = data;
    tail_head = node->next;
    node->next = tail;
    tail = node;
}

void SingleLinkList::traverse()
{
    SingleLinkListNode *temp = list->next;
    cout<<"traverse:"<<endl;
    while(temp != NULL){
        cout<<temp->data<<endl;
        temp = temp->next;
    }
}

void SingleLinkList::traverse_fortail()
{
    SingleLinkListNode *temp = tail_head;
    cout<<"traverse:"<<endl;
    cout<<"data"<<temp->data<<endl;
    while(temp != NULL){
        temp = temp->next;
        cout<<temp->data<<endl;
    }
}

void SingleLinkList::printTail()
{
    cout<<"tail:"<<tail->data<<endl;
}

LinkListType SingleLinkList::getNode(int pos)
{
    SingleLinkListNode *temp = list->next;
    int count = 0;
    if(pos <= 0){
        return temp->data;
    }

    while(temp != NULL && count < pos){
        temp = temp->next;
        count ++;
    }

//    if(temp != NULL)
        cout<<"node number = "<<count<<endl;
//    if(temp == NULL) // 不存在pos位置的节点
        cout<<"NO SUCH NODE! node number = "<<pos<<endl;


    return temp->data;
}
